﻿namespace FFF.Test.Application.AppServices.PublicService.Log
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading.Tasks;
    using Abp.Application.Services;
    using Abp.Auditing;
    using Abp.EntityFrameworkCore;
    using Abp.Runtime.Caching;
    using Abp.UI;
    using Abp.Web.Models;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Util.Web;
    using Ydm.Extensions;

    /// <summary>
    /// File的管理对象
    /// </summary>
    [YdmApiVersionAttribute(Version = "My")]
    public class LogAppService : YdmAbpAppServiceBase, ILogAppService
    {
        private readonly ICacheManager _cacheManager;
        private readonly IHttpContextAccessor _httpContext;
        private readonly IDbContextProvider<MyDbContext> _dbContextProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="LogAppService"/> class.
        /// File构造方法
        /// </summary>
        /// <param name="cacheManager">缓存服务</param>
        /// <param name="httpContext">context</param>
        /// <param name="dbContextProvider">dbcontext</param>
        public LogAppService(
            ICacheManager cacheManager,
            IHttpContextAccessor httpContext,
             IDbContextProvider<MyDbContext> dbContextProvider
            )
        {
            this._cacheManager = cacheManager;
            this._httpContext = httpContext;
            this._dbContextProvider = dbContextProvider;
        }

        /// <summary>
        /// 查看当天错误日志
        /// </summary>
        /// <param name="date">那个日期的数据(20190123)</param>
        /// <returns>文件dto</returns>
        [HttpGet]
        [WrapResult(WrapOnSuccess = false, WrapOnError = false)]
        [AllowAnonymous]
        public async Task<ContentResult> Error(string date = null)
        {
            if (date.IsNullOrEmpty())
            {
                date = DateTime.Now.ToString("yyyyMMdd");
            }

            string logfilePath = Server.MapPath($@"~\logs\Error\{date}.html");
            if (!File.Exists(logfilePath))
            {
                return null;
            }

            using (FileStream stream = File.Open(logfilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                int fsLen = (int)stream.Length;
                byte[] heByte = new byte[fsLen];
                int r = stream.Read(heByte, 0, heByte.Length);
                string myStr = Encoding.UTF8.GetString(heByte);
                myStr = myStr.Replace("\r\n", "<BR>");
                ContentResult contentResult = new ContentResult();
                contentResult.Content = myStr;
                contentResult.ContentType = "text/html; charset=UTF-8";
                return await Task.FromResult(contentResult);
            }
        }

        /// <summary>
        /// 查看当天的信息日志
        /// </summary>
        /// <param name="date">那个日期的数据(20190123)</param>
        /// <returns>文件html</returns>
        [HttpGet]
        [WrapResult(WrapOnSuccess = false, WrapOnError = false)]
        public async Task<ContentResult> Info(string date = null)
        {
            if (date.IsNullOrEmpty())
            {
                date = DateTime.Now.ToString("yyyyMMdd");
            }

            string logfilePath = Server.MapPath($@"~\logs\Info\{date}.html");
            using (FileStream stream = File.Open(logfilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                int fsLen = (int)stream.Length;
                byte[] heByte = new byte[fsLen];
                int r = stream.Read(heByte, 0, heByte.Length);
                string myStr = Encoding.UTF8.GetString(heByte);
                myStr = myStr.Replace("\r\n", "<BR>");
                ContentResult contentResult = new ContentResult();
                contentResult.Content = myStr;
                contentResult.ContentType = "text/html; charset=UTF-8";
                return await Task.FromResult(contentResult);
            }
        }

    }
}